package com.zhehekeji.opsys.api.webservice.server;

import javax.jws.WebService;

import org.apache.cxf.interceptor.InInterceptors;
import org.apache.cxf.interceptor.OutInterceptors;
import org.apache.cxf.transport.common.gzip.GZIPInInterceptor;
import org.apache.cxf.transport.common.gzip.GZIPOutInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.zhehekeji.opsys.api.code.LuckyVoteResultCode;
import com.zhehekeji.opsys.api.resp.CheckActivityResp;
import com.zhehekeji.opsys.api.resp.GetActivityListResp;
import com.zhehekeji.opsys.api.resp.GetActivityResp;
import com.zhehekeji.opsys.api.resp.GetDrawRecordCountByMobileResp;
import com.zhehekeji.opsys.api.resp.GetDrawRecordCountResp;
import com.zhehekeji.opsys.api.resp.GetDrawRecordResp;
import com.zhehekeji.opsys.api.resp.GetPrizeListResp;
import com.zhehekeji.opsys.api.resp.GetWinningResultListResp;
import com.zhehekeji.opsys.api.resp.LuckyDrawResp;
import com.zhehekeji.opsys.api.resp.UpdateWinningResultResp;
import com.zhehekeji.opsys.api.service.manager.ActivityManagerService;

/**
 * @ClassName ActivityManagerWS
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2015-4-7   上午10:33:22
 */
@InInterceptors(classes={GZIPInInterceptor.class})
@OutInterceptors(classes={GZIPOutInterceptor.class})
@WebService(endpointInterface="com.zhehekeji.opsys.api.webservice.server.ActivityManager",
name="activityManager",portName="activityManagerPort",serviceName="activityManagerWS")
public class ActivityManagerWS implements ActivityManager {
	
	private static final Logger log = LoggerFactory.getLogger(ActivityManagerWS.class);
	@Autowired
	private ActivityManagerService activityMangerService;
	@Override
	public CheckActivityResp checkActivity(String activityCode) {
		
		CheckActivityResp resp = activityMangerService.checkActivity(activityCode);
		return resp;
		
	}

	@Override
	public GetActivityResp getActivity(String activityCode) {

		GetActivityResp resp = activityMangerService.getActivity(activityCode);
		return resp;
	}

	@Override
	public GetPrizeListResp getPrizeList(String activityCode) {
		
		GetPrizeListResp resp = activityMangerService.getPrizeList(activityCode);
		return resp;
		
	}

	/*@Override
	public LuckyDrawResp luckyDraw(String activityCode, String userID) {
		
		LuckyDrawResp resp = activityMangerService.luckyDraw(activityCode, userID, -1);
		return resp;
		
	}*/

	@Override
	public LuckyDrawResp luckyDraw(String activityCode, String userID, int limitWinNum) {
		log.info("进入活动抽奖， activityCode="+activityCode+",userID="+userID+",limitWinNum="+limitWinNum);
		LuckyDrawResp resp = new LuckyDrawResp();
		try{
			resp = activityMangerService.luckyDraw(activityCode, userID, limitWinNum);
		}catch(Exception e1){
			log.error("", e1);
			//由javax.persistence.OptimisticLockException这里共享锁抛出的异常，再执行一次
			try{
				log.info(String.format("用户[%s]参与活动[%s]抽奖时异常，事务回滚，程序自动控制，再抽一次！", userID, activityCode));
				resp = activityMangerService.luckyDraw(activityCode, userID, limitWinNum);
			}catch(Exception e2){
				String errorDesc = String.format("用户[%s]参与活动[%s]抽奖时异常，事务回滚，抽奖失败！", userID, activityCode);
				resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
				resp.setResultDesc(errorDesc);
				log.error(errorDesc, e2);
			}
		}
		
		return resp;
	}

	@Override
	public GetDrawRecordResp getDrawRecord(String activityCode, int firstIndex, int maxResult) {
		
		GetDrawRecordResp resp = activityMangerService.getDrawRecord(activityCode, firstIndex, maxResult);
		return resp;
		
	}

	@Override
	public GetActivityListResp getActivityList(Long cpCode) {
		
		GetActivityListResp resp = activityMangerService.getActivityList(cpCode);
		return resp;
		
	}

	@Override
	public GetWinningResultListResp getWinningResultList(String activityCode, int firstIndex, int maxResult) {
		
		GetWinningResultListResp resp = activityMangerService.getWinningResultList(activityCode, firstIndex, maxResult);
		return resp;
		
	}

	@Override
	public GetWinningResultListResp getWinningResultListByPrizeCode(String prizeCode, int firstIndex, int maxResult) {
		
		GetWinningResultListResp resp = activityMangerService.getWinningResultListByPrizeCode(prizeCode, firstIndex, maxResult);
		return resp;
		
	}

	@Override
	public GetWinningResultListResp getWinningResultListByUserID(String userID, String activityCode,
			int firstIndex, int maxResult) {
		
		GetWinningResultListResp resp = activityMangerService.getWinningResultListByUserID(userID, activityCode, firstIndex, maxResult);
		return resp;
		
	}

	@Override
	public GetDrawRecordCountResp getDrawRecordCount(String activityCode, String userID) {
		
		GetDrawRecordCountResp resp = activityMangerService.getDrawRecordCount(activityCode, userID);
		return resp;
		
	}

	@Override
	public GetDrawRecordCountByMobileResp getDrawRecordCountByMobile(String activityCode, String mobileNo) {
		
		GetDrawRecordCountByMobileResp resp = activityMangerService.getDrawRecordCountByMobile(activityCode, mobileNo);
		return resp;
	}

	@Override
	public GetWinningResultListResp getWinningResultListByMobile(String mobileNo, String activityCode,
			int firstIndex, int maxResult) {
		GetWinningResultListResp resp = activityMangerService.getWinningResultListByMobile(mobileNo, activityCode, firstIndex, maxResult);
		return resp;
	}

	@Override
	public UpdateWinningResultResp updateWinningResult(Long winningResultId, String mobileNo) {
		UpdateWinningResultResp resp = activityMangerService.updateWinningResult(winningResultId, mobileNo);
		return resp;
	}

	
}
